package com.husd.framework.ddl_new;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * @author shengdonghu@126.com
 * @date 2024/1/22 18:19
 */
@Getter
public enum MysqlColumnDefine {

    bit0("bit", 0, "java.lang.Boolean"),
    bit1("bit", 1, "byte[]"),
    tinyint("tinyint", 0, "Integer"),
    bool("bool", 0, "Integer"),
    bool2("boolean", 0, "Integer"),
    smallint("smallint", 0, "Integer"),
    mediumint("mediumint", 0, "Integer"),

    int0("int", 0, "Integer"), // java.lang.Integer；无符的话是 java.lang.Long
    int1("int", 1, "java.lang.Long"), // java.lang.Integer；无符的话是 java.lang.Long

    integer0("integer", 0, "Integer"), // java.lang.Integer；无符的话是 java.lang.Long
    integer1("integer", 1, "java.lang.Long"), // java.lang.Integer；无符的话是 java.lang.Long
    bigint0("bigint", 0, "java.lang.Long"),
    bigint1("bigint", 1, " java.math.BigInteger"), // 无符的话是 java.math.BigInteger
    float0("bit", 0, "java.lang.Float"),
    double0("double", 0, "java.lang.Double"),
    decimal("decimal", 0, "java.math.BigDecimal"),
    date("date", 0, "java.util.Date"),
    datetime("datetime", 0, "String"),
    timestamp("timestamp", 0, "String"),
    time("time", 0, "String"),
    year("year", 0, "String"),
    char0("char", 0, "String"),
    varchar("varchar", 0, "String"),
    binary("binary", 0, "byte[]"),
    varbinary("varbinary", 0, "byte[]"),
    tinyblob("tinyblob", 0, "byte[]"),
    tinytext("tinytext", 0, "java.lang.String"),
    blob("blob", 0, "byte[]"),
    text("text", 0, "String"),
    mediumblob("mediumblob", 0, "byte[]"),
    mediumtext("mediumtext", 0, "java.lang.String"),
    longblob("longblob", 0, "byte[]"),
    longtext("bit", 0, "java.lang.String"),
    enum0("enum", 0, "java.lang.String"),
    set0("set", 0, "java.lang.String"),

    ;
    private String columnName;
    private int len;
    private String javaClass;

    MysqlColumnDefine(String columnName, int len, String javaClass) {
        this.columnName = columnName;
        this.len = len;
        this.javaClass = javaClass;
    }

    public static MysqlColumnDefine getMysqlColumnDefineByStr(String str, int len, boolean unsigned) {

        for (MysqlColumnDefine item : MysqlColumnDefine.values()) {

            String columnName = item.getColumnName();
            if (StringUtils.equals(str, columnName)) {
                if (StringUtils.equals(str, "bit") && len > 1) {
                    return MysqlColumnDefine.bit1;
                }
                if (StringUtils.equals(str, "int") && len > 11) {
                    return MysqlColumnDefine.bigint0;
                }
                if (StringUtils.equals(str, "int") && unsigned) {
                    return MysqlColumnDefine.int1;
                }
                if (StringUtils.equals(str, "integer") && unsigned) {
                    return MysqlColumnDefine.integer1;
                }
                if (StringUtils.equals(str, "bigint") && unsigned) {
                    return MysqlColumnDefine.bigint1;
                }
                return item;
            }
        }

        throw new RuntimeException("没有根据名字匹配到mysql的列 str:" + str + " len:" + len);
    }
}
